var $ = require("../../utils/Ajax.js");
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    // 购物车ID
    Cids: null,

    // 收货地址地址Id
    AddressId: 0,

    // 商品id属性id数量
    GidAidNum: [],

    OrderInfor: null,
    OrderNumber: null,
    OrderMoney: null,

    // 配送方式
    DistriIndex: 0,
    Distribution: ["快递免邮", "到店自提","我要存茶"],

    // 判断存茶地址是否显示
    TeaAddress: true,

    // 判断存茶信息是否显示
    TeaInfor: false,

    // 存茶时间
    TeaTime: "请选择存茶时间",
    TeaTimeId: 0,
    TeaMoney: 0,

    // 存茶
    showShopPopup: false,
    animationData: {},

    // 选择优惠券
    CouponId: 0,
    CouponSelect: "请选择优惠券",

    // 优惠券
    showShopPopup1: false,
    animationData1: {},

    // 是否可以开发票
    InvoiceId: 0,
    IfInvoice: "本次不开具发票",

    OrderInforShopPrice: 0
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "确认订单"
    })
    var OrderInfor = JSON.parse(options.OrderInfor);
    var OrderNumber = 0;
    var OrderMoney = 0;
    var GidAidNum = [];
    for (var i = 0; i < OrderInfor.carts.length;i++){
      OrderNumber += Number(OrderInfor.carts[i].box) * Number(OrderInfor.carts[i].num);
      OrderMoney += Number(OrderInfor.carts[i].price) * Number(OrderInfor.carts[i].num);
      GidAidNum.push(OrderInfor.carts[i].goodsId + "_" + OrderInfor.carts[i].attrId + "_" + OrderInfor.carts[i].num);
    }
    this.setData({
      AddressId: OrderInfor.address.id,
      GidAidNum: GidAidNum,
      Cids: OrderInfor.cartIds,
      OrderNumber: OrderNumber,
      OrderInforShopPrice: OrderMoney,
      OrderInfor: OrderInfor
    })
  },

  onShow: function () {
    var SubmitOrderAddress = wx.getStorageSync('SubmitOrderAddress');
    if (SubmitOrderAddress.length != 0) {
      var address = "OrderInfor.address";
      var addressId = "OrderInfor.address.id";
      this.setData({
        [address]: SubmitOrderAddress,
        AddressId: SubmitOrderAddress.id
      })
    }

    var Invoice = wx.getStorageSync('Invoice');
    if (Invoice.length != 0) {
      this.setData({
        InvoiceId: Invoice.id,
        IfInvoice: "电子(" + Invoice.content + "-" + Invoice.title + ")"
      })
    }else{
      this.setData({
        IfInvoice: "本次不开具发票"
      })
    }
  },

  // 修改收货地址
  EditAddress:function(){
    wx.navigateTo({
      url: '../ShippingAddress/ShippingAddress?QQ=812100002'
    })
  },

  // 点击优惠券
  Coupon: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(834).step()
    this.setData({
      animationData1: animation.export(),
      showShopPopup1: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData1: animation.export()
      })
    }, 200)
  },

  // 选择优惠券
  CouponSelect: function(e){
    var Id = e.detail.value.split("_");
    console.log(Id);
    this.setData({
      CouponId: Id[0],
      CouponSelect: Id[1]
    })
  },

  // 关闭优惠券
  CouponClose: function (e) {
    var Status = e.currentTarget.dataset.status;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(834).step()
    this.setData({
      animationData1: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData1: animation.export(),
        showShopPopup1: false
      })
    }.bind(this), 200)
    if (Status == 1){
      
    }else{
      this.setData({
        CouponSelect: "请选择优惠券"
      })
    }
  },

  // 点击选择配送方式
  ClickDistri: function(e){
    var Index = e.currentTarget.dataset.index_;
    var OrderMoney = 0;
    var Carts = this.data.OrderInfor.carts;
    for (var i = 0; i < Carts.length; i++) {
      OrderMoney += Number(Carts[i].price) * Number(Carts[i].num);
    }
    this.setData({
      DistriIndex: Index,
      OrderInforShopPrice: OrderMoney
    })
    switch (Index){
      case 0:
        this.setData({
          TeaAddress: true,
          DistriIndex: Index,
          TeaInfor: false,
          TeaTime: "请选择存茶时间",
          TeaTimeId: 0,
          TeaMoney: 0,
        })
        break;
      case 1:
        this.setData({
          TeaAddress: false,
          DistriIndex: Index,
          TeaInfor: false
        })
        break;
      case 2:
        this.setData({
          TeaNumber: false,
          DistriIndex: Index,
          TeaInfor: true
        })
        break;
    }
  },

  // 点击存茶时间
  StoreTea: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(452).step()
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 选择存茶时间
  TeaTimeSelect: function(e){
    var Id = e.detail.value.split("_");
    this.setData({
      TeaTimeId: Id[0],
      TeaTime: Id[1],
      TeaMoney: Id[2]
    })
  },

  // 关闭存茶时间
  StoreTeaClose: function (e) {
    var Status = e.currentTarget.dataset.status;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(452).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
    if (Status == 1){
      var TeaMoney = this.data.TeaMoney;
      var OrderMoney = 0;
      var OrderJian = 0;
      var Carts = this.data.OrderInfor.carts;
      for (var i = 0; i < Carts.length; i++) {
        OrderMoney += Number(Carts[i].price) * Number(Carts[i].num);
        OrderJian += Number(Carts[i].box) * Number(TeaMoney);
      }
      this.setData({
        OrderInforShopPrice: OrderMoney + OrderJian
      })
    }else{
      var OrderMoney = this.data.OrderMoney;
      this.setData({
        TeaTimeId: 0,
        TeaTime: "请选择存茶时间",
        TeaMoney: 0
      })
    }
  },

  // 点击发票
  Invoice: function(){
    wx.navigateTo({
      url: '../Invoice/Invoice',
    })
  },

  // 点击确认付款
  PaymentSubmit: function(){
    var that = this;
    var gan = that.data.GidAidNum;
    var costId = that.data.TeaTimeId;
    var couponId = that.data.CouponId;
    var invoiceId = that.data.InvoiceId;
    var cids = that.data.Cids;
    var orderType = that.data.DistriIndex;
    var addressId = that.data.AddressId;
    $.Ajax("api/makeOrder","POST",{
      gan: gan,
      costId: costId,
      couponId: couponId,
      invoiceId: invoiceId,
      cids: cids,
      kinds: 0,
      orderType: orderType,
      isActivity: 0,
      addressId: addressId
    },function(res){
      if (res.s) {
        wx.requestPayment({
          timeStamp: res.d.config.timestamp,
          nonceStr: res.d.config.nonceStr,
          package: res.d.config.package,
          signType: res.d.config.signType,
          paySign: res.d.config.paySign,
          success: function (res) {
            wx.navigateTo({
              url: '../MyOrder/MyOrder?Status=1',
            })
          },
          fail: function (res) {
            wx.showToast({
              title: "取消支付",
              icon: 'success',
              duration: 1500
            })
          }
        })
      } else {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})